package com.lagou.dubbo.customer.service;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.lagou.dubbo.api.RpcTestService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

@Component
public class RpcTestServiceImpl {

    @Reference
    private RpcTestService rpcTestService;


    private final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("pes-so-pool").build();
    private final ExecutorService threadPool = new ThreadPoolExecutor(50, 200,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue(1024), threadFactory, new ThreadPoolExecutor.AbortPolicy());



    public String sayHolle(String name) {

        return rpcTestService.sayHello(name);
    }



    @Async
    public void  testTp(){
        while (true){
            for (int i = 0; i < 100; i++) {
                TpTestThread thread = new TpTestThread(rpcTestService);
                threadPool.execute(thread);
            }
        }
    }

}
class TpTestThread implements Runnable {

    private RpcTestService rpcTestService;



    public TpTestThread(RpcTestService rpcTestService) {
        this.rpcTestService = rpcTestService;
    }

    public TpTestThread() {

    }

    @Override
    public void run() {
        System.out.println("通过线程池方式创建的线程：" + Thread.currentThread().getName() + " ");

        rpcTestService.methodA();

        rpcTestService.methodB();

        rpcTestService.methodC();
    }
}
